package com.winbaoxian.testng.utils;

import com.winbaoxian.testng.constant.WinTestNGConstant;
import com.winbaoxian.testng.model.core.ActionConfigDTO;
import com.winbaoxian.testng.model.core.TestCasesContext;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.stream.Collectors;

/**
 * @author dongxuanliang252
 * @date 2019-03-08 15:01
 */
public enum WinTestNGLogUtils {
    INSTANCE;

    public String getActionLogPrefix(ActionConfigDTO currentActionConfig, TestCasesContext context) {
        String prefix = getFlowLogPrefix(context);
        if (CollectionUtils.isNotEmpty(context.getActionConfigStack())) {
            List<String> actionNames = context.getActionConfigStack().stream().map(o -> String.format(WinTestNGConstant.LOG_PREFIX_ACTION, o.getName(), o.getActionType())).collect(Collectors.toList());
            prefix += WinTestNGConstant.CHAR_RIGHT_ARROW + StringUtils.join(actionNames, WinTestNGConstant.CHAR_RIGHT_ARROW);
        }
        prefix += WinTestNGConstant.CHAR_RIGHT_ARROW + String.format(WinTestNGConstant.LOG_PREFIX_ACTION, currentActionConfig.getName(), currentActionConfig.getActionType());
        return prefix;
    }

    public String getTLogPrefix(TestCasesContext context) {
        String prefix = String.format(WinTestNGConstant.LOG_PREFIX_UUID, context.getReportDataContext().getReportUuid());
        if (context.getTestCasesDTO() != null) {
            prefix += WinTestNGConstant.CHAR_COLON + String.format(WinTestNGConstant.LOG_PREFIX_TESTCASE, StringUtils.defaultIfBlank(context.getTestCasesDTO().getName(), context.getTestCasesDTO().getId().toString()));
        }
        return prefix;
    }

    public String getFlowLogPrefix(TestCasesContext context) {
        String prefix = getTLogPrefix(context);
        if (StringUtils.isNotBlank(context.getFlowShowTitle())) {
            prefix += WinTestNGConstant.CHAR_RIGHT_ARROW + String.format(WinTestNGConstant.LOG_PREFIX_FLOW, context.getFlowShowTitle());
        }
        return prefix;
    }

    public String[] getTestReportTitles(ActionConfigDTO currentActionConfig, TestCasesContext context) {
        List<String> titles = new ArrayList<>();
        Stack<ActionConfigDTO> templateNameStack = context.getActionConfigStack();
        if (CollectionUtils.isNotEmpty(templateNameStack)) {
            List<String> actionNames = context.getActionConfigStack().stream().map(o -> String.format(WinTestNGConstant.LOG_PREFIX_ACTION, o.getName(), o.getActionType())).collect(Collectors.toList());
            for (int i = 0; i < actionNames.size(); i++) {
                String title = StringUtils.join(actionNames.subList(0, i + 1), WinTestNGConstant.CHAR_RIGHT_ARROW);
                titles.add(title);
            }
        }
        titles.add(String.format(WinTestNGConstant.LOG_PREFIX_ACTION, currentActionConfig.getName(), currentActionConfig.getActionType()));
        return titles.toArray(new String[titles.size()]);
    }

    public String getErrorString(Throwable e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        return sw.toString();
    }


}
